package com.cookor.poi.modules.phy.schedule;

import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.WorkDayUtils;
import com.cookor.poi.modules.phy.entity.FSchedue;
import com.cookor.poi.modules.phy.entity.FSchedueTemplate;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.service.FSchedueService;
import com.cookor.poi.modules.phy.service.FSchedueTemplateService;
import com.cookor.poi.modules.phy.service.TOrderService;
import java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Lazy(false)
public class ShiftSchedule {
  private static final Logger logger = Logger.getLogger(ShiftSchedule.class);
  
  @Autowired
  private FSchedueTemplateService fSchedueTemplateService;
  
  @Autowired
  private FSchedueService fSchedueService;
  
  @Autowired
  private TOrderService tOrderService;
//  @Scheduled(cron = "0 */2 * * * ?")
  @Scheduled(cron = "0 0 10 21-31 * ?")
  @Transactional(readOnly = false, rollbackFor = {Exception.class})
  public void createFShift() throws Exception {
    logger.info("=============排班定时器启动=============");
    List<FSchedueTemplate> templateList = this.fSchedueTemplateService.findList(new FSchedueTemplate());
    Integer year = Integer.parseInt(DateUtils.getYear());
    Integer month = Integer.parseInt(DateUtils.getMonth());
    Integer now_month = month;
    Integer now_year = year;
    FSchedue queryFSchedue = new FSchedue();
    queryFSchedue.setScheduleMonth(now_month);
    queryFSchedue.setScheduleYear(now_year);
    List<FSchedue> fSchedueList = this.fSchedueService.findList(queryFSchedue);
    if (fSchedueList.isEmpty()) {
      Calendar c = Calendar.getInstance();
      c.set(5, 1);
      Integer cMonth = c.get(2);
      while (c.get(Calendar.MONTH) != now_month) {
        System.out.println("1");
        int n = 1;
        int nowDay = c.get(5);
        String weekStr = WorkDayUtils.getChineseWeek(c);
        FSchedue fSchedue = new FSchedue();
        for (FSchedueTemplate template : templateList) {
          fSchedue.setTenantId(template.getTenantId());
          fSchedue.setScheduleYear(now_year);
          fSchedue.setScheduleMonth(now_month);
          fSchedue.setScheduleDay(nowDay);
          fSchedue.setWeekStr(weekStr);
          if (n == 1) {
            fSchedue.setTitleA(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setTotalA(Integer.valueOf(0));
            } else {
              fSchedue.setTotalA(template.getCount());
            } 
            fSchedue.setBeginDateA(template.getBeginDate());
            fSchedue.setEndDateA(template.getEndDate());
          } else if (n == 2) {
            fSchedue.setTitleB(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setTotalB(Integer.valueOf(0));
            } else {
              fSchedue.setTotalB(template.getCount());
            } 
            fSchedue.setBeginDateB(template.getBeginDate());
            fSchedue.setEndDateB(template.getEndDate());
          } else if (n == 3) {
            fSchedue.setTitleC(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setTotalC(Integer.valueOf(0));
            } else {
              fSchedue.setTotalC(template.getCount());
            } 
            fSchedue.setBeginDateC(template.getBeginDate());
            fSchedue.setEndDateC(template.getEndDate());
          } else if (n == 4) {
            fSchedue.setTitleD(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setTotalD(Integer.valueOf(0));
            } else {
              fSchedue.setTotalD(template.getCount());
            } 
            fSchedue.setBeginDateD(template.getBeginDate());
            fSchedue.setEndDateD(template.getEndDate());
          } else if (n == 5) {
            fSchedue.setTitleE(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setTotalE(Integer.valueOf(0));
            } else {
              fSchedue.setTotalE(template.getCount());
            } 
            fSchedue.setBeginDateE(template.getBeginDate());
            fSchedue.setEndDateE(template.getEndDate());
          } else {
            continue;
          } 
          n++;
        } 
        this.fSchedueService.save(fSchedue);
        c.add(5, 1);
        cMonth = Integer.valueOf((c.get(2) == 0) ? 12 : c.get(2));
      } 
    } 
    if (month.intValue() == 12) {
      month = Integer.valueOf(1);
      year = Integer.valueOf(year.intValue() + 1);
    } else {
      month = Integer.valueOf(month.intValue() + 1);
    } 
    queryFSchedue = new FSchedue();
    queryFSchedue.setScheduleMonth(month);
    queryFSchedue.setScheduleYear(year);
    fSchedueList = this.fSchedueService.findList(queryFSchedue);
    if (fSchedueList.size() == 0) {
      Calendar c = Calendar.getInstance();
      c.add(2, 1);
      c.set(5, 1);
      Integer cMonth = Integer.valueOf(c.get(2));
      while (cMonth != Integer.valueOf(month.intValue())) {
        System.out.println("2");
        System.out.println(c.get(2));
        int n = 1;
        int nowDay = c.get(5);
        String weekStr = WorkDayUtils.getChineseWeek(c);
        FSchedue fSchedue = new FSchedue();
        for (FSchedueTemplate template : templateList) {
          fSchedue.setTenantId(template.getTenantId());
          fSchedue.setScheduleYear(year);
          fSchedue.setScheduleMonth(month);
          fSchedue.setScheduleDay(Integer.valueOf(nowDay));
          fSchedue.setWeekStr(weekStr);
          if (n == 1) {
            fSchedue.setTitleA(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setCountA(Integer.valueOf(0));
              fSchedue.setTotalA(Integer.valueOf(0));
            } else {
              fSchedue.setCountA(template.getCount());
              fSchedue.setTotalA(template.getCount());
            } 
            fSchedue.setBeginDateA(template.getBeginDate());
            fSchedue.setEndDateA(template.getEndDate());
          } else if (n == 2) {
            fSchedue.setTitleB(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setCountB(Integer.valueOf(0));
              fSchedue.setTotalB(Integer.valueOf(0));
            } else {
              fSchedue.setCountB(template.getCount());
              fSchedue.setTotalB(template.getCount());
            } 
            fSchedue.setBeginDateB(template.getBeginDate());
            fSchedue.setEndDateB(template.getEndDate());
          } else if (n == 3) {
            fSchedue.setTitleC(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setCountC(Integer.valueOf(0));
              fSchedue.setTotalC(Integer.valueOf(0));
            } else {
              fSchedue.setCountC(template.getCount());
              fSchedue.setTotalC(template.getCount());
            } 
            fSchedue.setBeginDateC(template.getBeginDate());
            fSchedue.setEndDateC(template.getEndDate());
          } else if (n == 4) {
            fSchedue.setTitleD(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setCountD(Integer.valueOf(0));
              fSchedue.setTotalD(Integer.valueOf(0));
            } else {
              fSchedue.setCountD(template.getCount());
              fSchedue.setTotalD(template.getCount());
            } 
            fSchedue.setBeginDateD(template.getBeginDate());
            fSchedue.setEndDateD(template.getEndDate());
          } else if (n == 5) {
            fSchedue.setTitleE(template.getTitle());
            if (isRest(template, weekStr)) {
              fSchedue.setCountE(Integer.valueOf(0));
              fSchedue.setTotalE(Integer.valueOf(0));
            } else {
              fSchedue.setCountE(template.getCount());
              fSchedue.setTotalE(template.getCount());
            } 
            fSchedue.setBeginDateE(template.getBeginDate());
            fSchedue.setEndDateE(template.getEndDate());
          } else {
            continue;
          } 
          n++;
        } 
        this.fSchedueService.save(fSchedue);
        c.add(5, 1);
        cMonth = Integer.valueOf((c.get(2) == 0) ? 12 : c.get(2));
      } 
    } 
    logger.info("=============排班定时器结束=============");
  }
  
  private boolean isRest(FSchedueTemplate template, String weekStr) {
    if (weekStr.equals("星期一"))
      return (template.getMon().intValue() == 0); 
    if (weekStr.equals("星期二"))
      return (template.getTue().intValue() == 0); 
    if (weekStr.equals("星期三"))
      return (template.getWed().intValue() == 0); 
    if (weekStr.equals("星期四"))
      return (template.getThu().intValue() == 0); 
    if (weekStr.equals("星期五"))
      return (template.getFri().intValue() == 0); 
    if (weekStr.equals("星期六"))
      return (template.getSat().intValue() == 0); 
    if (weekStr.equals("星期日"))
      return (template.getSun().intValue() == 0); 
    return false;
  }
  
  @Scheduled(cron = "0 */5 * * * ?")
  @Transactional(readOnly = false, rollbackFor = {Exception.class})
  public void updateOrderSstatus() throws Exception {
    logger.info("=============订单定时器启动=============");
    TOrder query = new TOrder();
    query.setStatus(Integer.valueOf(1));
    List<TOrder> list = this.tOrderService.findList(query);
    Calendar calendar = Calendar.getInstance();
    int nowHour = calendar.get(11);
    int nowMin = calendar.get(12);
    for (TOrder order : list) {
      if (DateUtils.isSameDay(order.getBookDate(), calendar.getTime())) {
        String officeName = order.getOfficeName();
        String hourStr = officeName.split("-")[1].split(":")[0];
        if (hourStr.indexOf("0") == 0)
          hourStr.substring(1); 
        int hour = Integer.valueOf(hourStr).intValue();
        if (nowHour > hour) {
          order.setStatus(Integer.valueOf(10));
          this.tOrderService.save(order);
        } 
        if (nowHour == hour) {
          String minStr = officeName.split("-")[0].split(":")[1];
          if (nowMin > Integer.valueOf(minStr).intValue()) {
            order.setStatus(Integer.valueOf(10));
            this.tOrderService.save(order);
          } 
        } 
      } 
    } 
    logger.info("=============订单定时器结束=============");
  }
}
